package com.muslimedia.reloadmanager.data;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts.GroupMembership;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Groups;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import android.widget.Toast;

public class CustomerDataSource {
	Context context;
	
	public CustomerDataSource(Context context){
		this.context = context;
	}
	
	public long getContactId(String phoneNumber){
		ContentResolver contentResolver = context.getContentResolver();

		Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

		String[] projection = new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID};

		Cursor cursor =  
		   contentResolver.query( uri, projection, null, null, null);

		if(cursor!=null) {
		  while(cursor.moveToNext()){
		    String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
		    String contactId = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
		    
		    Log.d("windu-test", "contactMatch name: " + contactName);
		    Log.d("windu-test", "contactMatch id: " + contactId);
		    return Long.parseLong(contactName);
		  }
		  cursor.close();
		}
		return 0;
	}
		
	public void getContact(String phoneNumber){
		ContentResolver contentResolver = context.getContentResolver();
		Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
		String[] projection = new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID};
		Cursor cursor =  contentResolver.query( uri, projection, null, null, null);
		if(cursor!=null) {
		  while(cursor.moveToNext()){
		    String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
		    String contactId = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
		    Log.d("windu-test", "contactMatch name: " + contactName);
		    Log.d("windu-test", "contactMatch id: " + contactId);
		  }
		  cursor.close();
		}
	}
	
	public void getGroups(){
		ContentResolver contentResolver = context.getContentResolver();
		Uri uri = ContactsContract.Groups.CONTENT_URI;
		String[] projection = new String[] {Groups._ID, Groups.TITLE};
		Cursor cursor =  contentResolver.query( uri, projection, null, null, null);
		String s = "";
		if(cursor!=null) {
		  while(cursor.moveToNext()){
		    String groupId = cursor.getString(cursor.getColumnIndexOrThrow(Groups._ID));
		    String GroupName = cursor.getString(cursor.getColumnIndexOrThrow(Groups.TITLE));
		    s += "Group ID: "+groupId + " => ";
		    s += "Group Name: "+GroupName+"\n";
		  }
		  cursor.close();
		}
		Toast.makeText(context, s, Toast.LENGTH_LONG).show();
	}
	
	public long getGroupId(String groupName){
		long groupID = 0;
		ContentResolver contentResolver = context.getContentResolver();
		Uri uri = ContactsContract.Groups.CONTENT_URI;
		String[] projection = new String[] {Groups._ID};
		Cursor cursor =  contentResolver.query( uri, projection, Groups.TITLE + "=" + groupName, null, null);
		String s = "";
		if(cursor!=null) {
		  while(cursor.moveToNext()){
		    String groupId = cursor.getString(cursor.getColumnIndexOrThrow(Groups._ID));
		    groupID = Long.parseLong(groupId);
		  }
		  cursor.close();
		}
		return groupID;
	}
	
	public boolean isMemberGroup(long groupID){
		ContentResolver contentResolver = context.getContentResolver();
		Uri groupURI = ContactsContract.Data.CONTENT_URI;
	    String[] projection = new String[]{
	    		ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,
	    		//ContactsContract.CommonDataKinds.GroupMembership.DISPLAY_NAME,
	    		ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID
	    		};
	    Cursor c = contentResolver.query(groupURI, projection,
	    		ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID+"="+groupID,
	    		null,null);
	    String s = "";
	    while(c.moveToNext()){
	    	String g = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID));
	    	s += "Name : "+g;
	    	s += "\n";
	    }
	    Toast.makeText(context, s, Toast.LENGTH_LONG).show();
	    return true;
	}
}
